//
//  ClearCacheAutoViewController.m
//  iRSSNews
//
//  Created by bk_sport on 12/30/12.
//  Copyright (c) 2012 Mobioneer Ltd. All rights reserved.
//

#import "ClearCacheAutoViewController.h"
#import "AppDelegate.h"
#import "SettingsViewController.h"

@interface ClearCacheAutoViewController ()

@property (nonatomic, strong) NSArray *arrayTimeToClear;
@property (nonatomic, strong) NSString *stringTimeToClear;

@end

@implementation ClearCacheAutoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"ClearCacheAutoKey", @"");
    }
    return self;
}

- (void)viewDidLoad
{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:NSLocalizedString(@"DoneKey", @"") style:UIBarButtonItemStyleBordered target:self action:@selector(buttonDoneClick:)];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(void) buttonDoneClick:(id) sender
{
    
    if ([self.stringTimeToClear length] != 0) {
        // create a standardUserDefaults variable
        NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
        // saving an NSString
        [standardUserDefaults setObject:self.stringTimeToClear forKey:@"timeToClear"];
        // synchronize the settings
        [standardUserDefaults synchronize];
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(getTimeToClear)])
    {
        [self.delegate getTimeToClear];
    }
    
    [self.navigationController popToRootViewControllerAnimated:YES];

}


#pragma mark - UITableViewDataSource, UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 9;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.arrayTimeToClear = [[NSArray alloc] init];
    self.arrayTimeToClear = @[NSLocalizedString(@"NoneKey", @""), NSLocalizedString(@"AWeekKey", @""),NSLocalizedString(@"TwoWeeksKey", @""),NSLocalizedString(@"AMonthKey", @""),NSLocalizedString(@"ThreeMonthsKey", @""),NSLocalizedString(@"SixMonthsKey", @""),NSLocalizedString(@"AYearKey", @""),NSLocalizedString(@"TwoYearsKey", @""),NSLocalizedString(@"ThreeYearsKey", @""),];

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    cell.accessoryType = UITableViewCellAccessoryNone;
    NSInteger index = [indexPath row];
    
    cell.textLabel.text = [self.arrayTimeToClear objectAtIndex:index];
    
    // create a standardUserDefaults variable
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
    // getting an NSString object
    NSString *myString = [standardUserDefaults stringForKey:@"timeToClear"];    
    
    if ([cell.textLabel.text isEqualToString:myString])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return NSLocalizedString(@"ChooseTimeToClearKey", @"");
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 25;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    // create a standardUserDefaults variable
    NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
    // getting an NSString object
    NSString *preString = [standardUserDefaults stringForKey:@"timeToClear"];
    
    for (UITableViewCell *preCell in [tableView visibleCells]) {
        if ([preCell.textLabel.text isEqualToString:preString]) {
            preCell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    
    if ([self.stringTimeToClear length] != 0)
    for (UITableViewCell *tempCell in [tableView visibleCells]) {
        if ([tempCell.textLabel.text isEqualToString:self.stringTimeToClear]) {
            tempCell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
    
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    
    self.stringTimeToClear = cell.textLabel.text;
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
